const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

app.get('/api/calculate', (req, res) => {
    const { loanAmount, interestRate, loanTerm } = req.query;
    const monthlyInterestRate = interestRate / 12;
    const totalPayments = loanTerm;

    // Calculate monthly payment using the formula for an annuity
    const monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - Math.pow(1 + monthlyInterestRate, -totalPayments));
    const totalPayment = monthlyPayment * totalPayments;
    const totalInterest = totalPayment - loanAmount;

    res.json({
        monthlyPayment: monthlyPayment.toFixed(2),
        totalPayment: totalPayment.toFixed(2),
        totalInterest: totalInterest.toFixed(2)
    });
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});